import sys

class Solution(object):
    def removeDuplicateLetters(self, s):
        """
        :type s: str
        :rtype: str
        """
        if len(s)==0: return ''
        # find the last position of all letters from a to z
        pos = [sys.maxsize] * 26
        for i, c in enumerate(s):
            index = ord(c)-ord('a')
            pos[index] = i
        ch = min(s[:min(pos)+1])
        second = s[s.find(ch)+1:].replace(ch, '')
        # print(first, second, ch)
        return ch + self.removeDuplicateLetters(second)

print(Solution().removeDuplicateLetters('cbacdcbc'))